package com.ytjj.qmyx.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.config.CommonPropertiesConfig;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.ProductCategoryRequest;
import com.ytjj.qmyx.admin.model.response.ProductCategoryResp;
import com.ytjj.qmyx.admin.model.response.ProductCategoryResponse;
import com.ytjj.qmyx.admin.service.CategoryNowService;
import com.ytjj.qmyx.admin.service.ProductCategoryService;
import com.ytjj.qmyx.admin.service.channelProductService.ChannelProductService;
import com.ytjj.qmyx.mall.model.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商品分类管理
 */
@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private CommonPropertiesConfig commonPropertiesConfig;

    @Autowired
    private CategoryNowService categoryNowService;

    @Autowired
    private ChannelProductService channelProductService;

    /**
     * 查询商品分类列表
     * @param productCategoryRequest
     * @return
     */
    @PostMapping("/list")
    public CommonResult list(@RequestBody ProductCategoryRequest productCategoryRequest){
        CommonPage<ProductCategoryResponse> pageList = productCategoryService.list(productCategoryRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 新增分类表
     * @param productCategory
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody ProductCategory productCategory){
        return channelProductService.create(productCategory);
    }

    /**
     * 修改分类表
     * @param productCategory
     * @return
     */
    @PostMapping("/update")
    @OperLog(operModule = "商品模块", operDesc = "修改商品分类表", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult update(@RequestBody ProductCategory productCategory){
        return channelProductService.update(productCategory);
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @OperLog(operModule = "商品模块", operDesc = "根据id删除数据", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult delete(@RequestParam Integer id){
        return channelProductService.delete(id);
    }

    /**
     * 根据主键查询数据
     * @param id
     * @return
     */
    @GetMapping("/getProductCategory")
    public CommonResult getProductCategory(@RequestParam Integer id){
        return CommonResult.success(productCategoryService.getProductCategory(id));
    }

    /**
     * 查询1级分类--分页
     * @return
     */
    @PostMapping("/getParentList")
    public CommonResult getParentList(@RequestBody ProductCategoryRequest pcRequest){
        return channelProductService.getParentList(pcRequest);
    }

    /**
     * 增加商品时分类Id--一级分类数据
     * @return
     */
    @PostMapping("/getFirstList")
    public CommonResult getFirstList(){
        return CommonResult.success(productCategoryService.getFirstList());
    }

    /**
     * 增加商品时分类Id--二级分类数据
     * @return
     */
    @GetMapping("/getSecondList")
    public CommonResult getSecondList(@RequestParam Integer id){
        return CommonResult.success(productCategoryService.getSecondList(id));
    }

    /**
     * 查询不是1级分类
     * @return
     */
    @GetMapping("/getChildrens")
    public CommonResult getChildrens(){
        List<ProductCategory> productCategorys = productCategoryService.getChildrens();
        return CommonResult.success(productCategorys);
    }

    /**
     * 根据1级分类查询对应的分类信息
     * @return
     */
    @PostMapping("/getChildrensById")
    public CommonResult getChildrensById(@RequestBody ProductCategoryRequest pcRequest){
        return channelProductService.getChildrensById(pcRequest);
    }

    /**
     * 新增三级分类
     */
    @PostMapping("/addBottomCategory")
    public CommonResult addBottomCategory(@RequestBody ProductCategoryRequest productCategoryRequest) {
        productCategoryService.addBottomCategory(productCategoryRequest);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 查看所有三级分类信息
     */
    @PostMapping("/getBottomCategoryList")
    public CommonResult getBottomCategoryList(@RequestBody ProductCategoryRequest productCategoryRequest) {
        return channelProductService.getBottomCategoryList(productCategoryRequest);


    }

    /**
     * 编辑三级分类信息
     */
    @GetMapping("/editBottomCategory")
    public CommonResult editBottomCategory(@RequestParam("id") Integer id,
                                           @RequestParam("title") String title,
                                           @RequestParam("status") Byte status) {
        productCategoryService.editBottomCategory(id, title, status);
        return CommonResult.success(ResultCode.SUCCESS);
    }

    /**
     * 删除三级分类
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteBottomCategory")
    public CommonResult deleteBottomCategory(@RequestParam("id") Integer id) {
        productCategoryService.deleteBottomCategory(id);
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
     * 根据分类id查询属性集合
     * @param categoryId
     * @return
     */
    @GetMapping("/getSpecListByCategoryId")
    public CommonResult getSpecListByCategoryId(@RequestParam Integer categoryId) {
        List<Map<String,Object>> specLists = productCategoryService.getSpecListByCategoryId(categoryId);
        return CommonResult.success(specLists);
    }

    /**
     * 展示所有分类
     */
    @GetMapping("/showAllCategory")
    public CommonResult showAllCategory(){
        List<ProductCategoryResp> list = productCategoryService.showAllCategory();
        return CommonResult.success(list);
    }
}
